//
//  ILGSharePanel.m
//  WangHuo
//
//  Created by zhangtongle-Pro on 2017/7/10.
//  Copyright © 2017年 ifeng. All rights reserved.
//

#import "ILGSharePanel.h"

typedef NS_ENUM(NSInteger, ILGShareType) {
    ILGShareTypeWeixinFriend,
    ILGShareTypeWeiXinSpace,
    ILGShareTypeCopyLink
};

@interface ILGShareItemCell : UICollectionViewCell
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleL;
@end
@implementation ILGShareItemCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    CGFloat imageSize = 44;
    CGFloat titleFontNum = 12;
    CGFloat itemInterSpace = 5;
    
    CGFloat imageX = (self.frame.size.width - imageSize)/2.0;
    CGFloat imageY = (self.frame.size.height - imageSize - itemInterSpace - titleFontNum)/2.0;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageSize, imageSize)];
    [self.contentView addSubview:imageView];
    _imageView = imageView;
    
    CGFloat titleY = imageView.frame.origin.y + imageView.frame.size.height + itemInterSpace;
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, titleY, self.frame.size.width, titleFontNum)];
    titleL.textAlignment = NSTextAlignmentCenter;
    titleL.textColor = [UIColor ilg_colorWithHexString:@"666666"];
    titleL.font = [UIFont systemFontOfSize:titleFontNum];
    [self.contentView addSubview:titleL];
    _titleL = titleL;
}

@end


@interface ILGSharePanel () <UIAlertViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) ILGShareContent *shareContent;
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ILGSharePanel

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

+ (instancetype)view {
    ILGSharePanel *resultView = [[self alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 49,  [UIScreen mainScreen].bounds.size.width, 0)];
    resultView.shareContent = [[ILGShareContent alloc] init];
    return resultView;
}

#pragma mark - Getter

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    
    [_dataArr removeAllObjects];
    
    if (![ILGThirdDelegate isWXAppInstalled]) {
        [_dataArr addObject:@{@"icon":@"sharePanel_wxSpace", @"name":@"朋友圈", @"type":@(ILGShareTypeWeiXinSpace)}];
        [_dataArr addObject:@{@"icon":@"sharePanel_wxFriend", @"name":@"微信好友", @"type":@(ILGShareTypeWeixinFriend)}];
    }
    
    [_dataArr addObject:@{@"icon":@"sharePanel_link", @"name":@"复制链接", @"type":@(ILGShareTypeCopyLink)}];
    
    return _dataArr;
}

#pragma mark - UI

- (void)createUI {
    self.backgroundColor = [UIColor whiteColor];
    
    int numPerLine = 4;
    CGFloat itemHeight = 90;
    
    UILabel *shareTitleL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
    shareTitleL.textColor = [UIColor ilg_colorWithHexString:@"666666"];
    shareTitleL.textAlignment = NSTextAlignmentCenter;
    shareTitleL.text = @"分享到";
    [self addSubview:shareTitleL];
    
    CGFloat itemWidth = self.frame.size.width/numPerLine;
    
    CGFloat collectionViewHeight = (self.dataArr.count/numPerLine + (self.dataArr.count%numPerLine ? 1:0)) * itemHeight;
    
    CGRect colloctionViewFrame = self.bounds;
    colloctionViewFrame.origin.y = shareTitleL.frame.size.height;
    colloctionViewFrame.size.height = collectionViewHeight;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:colloctionViewFrame collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[ILGShareItemCell class] forCellWithReuseIdentifier:@"ILGShareItemCell"];
    [self addSubview:_collectionView];
    
    self.ilg_height = shareTitleL.frame.size.height + collectionViewHeight;
}

- (void)showFromBottom {
    [self.collectionView reloadData];
    
    [super showFromBottom];
}


- (void)setShareData:(NSDictionary *)shareData {
    _shareData = shareData;
    
    self.shareContent.contentType = ILGShareContentTypeLink;

    self.shareContent.thumbImageURL = shareData[@"thumb"];
    self.shareContent.contentURL = shareData[@"url"];
    self.shareContent.contentTitle = shareData[@"title"]?:@"您关注的主播直播啦，不要错过任何一次精彩哟";
    self.shareContent.contentDescription = shareData[@"des"];
}


#pragma mark - Event

- (IBAction)shareInWXSpace:(id)sender {
    self.shareContent.shareType = ILGThirdTypeWeiXin;
    self.shareContent.scene = 1;
    
    [self share];
}
- (IBAction)shareInWXFriend:(id)sender {
    self.shareContent.shareType = ILGThirdTypeWeiXin;
    self.shareContent.scene = 0;

    [self share];
}
- (IBAction)shareInQQFriend:(id)sender {
    self.shareContent.shareType = ILGThirdTypeQQ;
    self.shareContent.scene = 0;
    
    [self share];
}
- (IBAction)shareInQQSpace:(id)sender {
    self.shareContent.shareType = ILGThirdTypeQQ;
    self.shareContent.scene = 1;

    [self share];
}
- (IBAction)shareInWeiBo:(id)sender {
    self.shareContent.shareType = ILGThirdTypeWeiBo;

    [self share];
}
- (IBAction)copyLink:(id)sender {
    [self hideFromBottom];

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    if (self.shareData[@"url"]) {
        pasteboard.string = self.shareData[@"url"];
    }
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享" message:@"分享主播" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去QQ粘贴", @"去微信粘贴", nil];
    [alertView show];
}

- (void)share {
    [self hideFromBottom];
    
//    __weak typeof(self)weakSelf = self;
    [[ILGShare shareInstance] share:self.shareContent callback:^(NSError *error, NSDictionary *result) {
        if (error) {
            [[UIApplication sharedApplication].keyWindow ilg_makeToast:@"分享失败"];
        } else {
//            [weakSelf hideFromBottom];
            [[UIApplication sharedApplication].keyWindow ilg_makeToast:@"分享成功"];
        }
    }];
}

#pragma mark - Delegate

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) { //QQ
//        [ILGThirdDelegate openQQ];
    } else if (buttonIndex == 2) { //微信
//        [ILGThirdDelegate openWeiXin];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ILGShareItemCell *cell = (ILGShareItemCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ILGShareItemCell" forIndexPath:indexPath];
    NSDictionary *dataDic = [self.dataArr objectAtIndex:indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:dataDic[@"icon"]];
    cell.titleL.text = dataDic[@"name"];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArr[indexPath.row];
    ILGShareType shareType = [dic[@"type"] integerValue];
    switch (shareType) {
        case ILGShareTypeWeixinFriend:
            [self shareInWXFriend:nil];
            break;
        case ILGShareTypeWeiXinSpace:
            [self shareInWXSpace:nil];
            break;
        case ILGShareTypeCopyLink:
            [self copyLink:nil];
            break;
        default:
            break;
    }
}

@end
